﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;



public class FPSCounter : MonoBehaviour
{
	const float fpsMeasurePeriod = 0.5f;
	private int m_FpsAccumulator = 0;
	private float m_FpsNextPeriod = 0;
	private int m_CurrentFps;
	const string display = "{0} FPS";


	private GUIStyle style = new GUIStyle ();

	private void Start()
	{
		style.fontSize = 26;
		style.normal.textColor = Color.white;
		m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
	}



	void OnGUI ()
	{
		GUI.Label (new Rect (10, 5, 50, 20), m_CurrentFps.ToString ("#,##0.0 fps"), style);
	}

	private void Update()
	{
		// measure average frames per second
		m_FpsAccumulator++;
		if (Time.realtimeSinceStartup > m_FpsNextPeriod)
		{
			m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
			m_FpsAccumulator = 0;
			m_FpsNextPeriod += fpsMeasurePeriod;

		}
	}
}